import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_app/utils/AppUtil.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter_app/components/layout.dart';
import 'package:flutter_app/generated/l10n.dart';
import './remind.dart';
class ChargePage extends StatefulWidget {
  @override
  _ChargePageState createState() => _ChargePageState();
}

Widget CoinItem(item) {
  return Row(
    children: [
      Image.asset('assets/images/coins/ic_usdc.png', width: 25),
      SizedBox(
        width: 10.0,
      ),
      Expanded(
        child: Container(
            padding: EdgeInsets.symmetric(vertical: 20.0),
            decoration: BoxDecoration(
                border: Border(
              bottom: BorderSide(color: Colors.grey.withOpacity(0.13)),
            )),
            child: Text('BTC')),
      )
    ],
  );
}

List<Widget> getCoins() {
  return [1, 1, 1, 1].map((item) {
    return CoinItem(item);
  }).toList();
}

Widget ButtonItem({Function onPressed, String text}) {
  return FlatButton(
      height: 40,
      color: Colors.blueGrey.withOpacity(0.06),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
      onPressed: onPressed,
      child: Text(
        text,
        style: TextStyle(color: Colors.blueAccent),
      ));
}

class _ChargePageState extends State<ChargePage> {
  String address = '0XSHI908888...XHSAKSH7782BHSAHI9088';
  GlobalKey repaintWidgetKey = GlobalKey(); // 绘图key值
  void _selectCoin() {
    GetBottomSheet(context,
        height: MediaQuery.of(context).size.height - 26,
        //选择币种
        text: S.of(context).select_coin,
        children: getCoins());
  }

  void copy() {
    AppUtil.copy(address);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //充币
        appBar: DarkHeadBar(S.of(context).deposit),
        body: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 15.0),
          child: Column(
            children: [
              Container(
                child: Column(
                  children: [
                    SizedBox(height: 25),
                    InkWell(
                      onTap: _selectCoin,
                      child: Container(
                        padding:
                            EdgeInsets.only(left: 15.0, top: 5.0, bottom: 5.0),
                        decoration: BoxDecoration(
                            color: Colors.grey.withOpacity(0.09),
                            borderRadius: BorderRadius.circular(4)),
                        child: RowBetween(
                          children: [
                            Row(
                              children: [
                                Image.asset(
                                  "assets/images/ic_btc.png",
                                  width: 30,
                                  height: 30,
                                ),
                                SizedBox(
                                  width: 10,
                                ),
                                Text('USDC', style: TextStyle(fontSize: 16))
                              ],
                            ),
                            Icon(
                              Icons.arrow_drop_down_rounded,
                              color: Colors.black,
                              size: 34,
                            )
                          ],
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    Container(
                      padding: EdgeInsets.all(19.0),
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          // alignment: Alignment(0.9, 0.5),
                          image:
                              AssetImage("assets/images/bg_qrcode_frame.png"),
                          fit: BoxFit.contain,
                        ),
                      ),
                      child: RepaintBoundary(
                          key: repaintWidgetKey,
                          child: QrImage(
                            //二维码设置前后背景色，不设置的情况下保存图片后因为是png格式的，白色透明的地方都是黑色的，导致查看图片时就是一个黑图
                            foregroundColor: Colors.black, //前景色
                            backgroundColor: Colors.white, //背景色
                            data: "123333",
                            size: 160,
                          )),
                    ),
                    SizedBox(height: 20),
                    Row(
                      children: [
                        Expanded(
                          child: Container(
                            alignment: Alignment.center,
                            padding: EdgeInsets.symmetric(
                                horizontal: 15.0, vertical: 15.0),
                            decoration: BoxDecoration(
                              color: Colors.grey.withOpacity(0.07),
                              borderRadius: BorderRadius.circular(100),
                            ),
                            child: Text(
                              address,
                              style: TextStyle(color: Colors.black87),
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: ButtonItem(
                                onPressed: () {
                                  saveQR(repaintWidgetKey);
                                },
                                //保存二维码
                                text: S.of(context).save_code),
                          ),
                          SizedBox(
                            width: 18,
                          ),
                          Expanded(
                            child: ButtonItem(
                              onPressed: copy,
                              //复制地址
                              text: S.of(context).copy_add,
                            ),
                          ),
                        ])
                  ],
                ),
              ),
              SizedBox(height: 25),
              Remind()
            ],
          ),
        )
    );
  }

  ///保存二维码
  void saveQR(GlobalKey repaintWidgetKey) async {
    // Navigator.push(context, MaterialPageRoute(builder: (context) {
    //   return new QRViewPage();
    // })).then((value) => {print("扫描页面结果返回 = $value")});
    Uint8List imageBytes =
        await AppUtil.capturePngToUint8List(repaintWidgetKey);
    AppUtil.saveImage(imageBytes);
  }
}
